package educrypt.commons.util;

public class TextUtil {
	public static String removeWhiteSpaces(String data) {
		String textWithoutSpaces = data.replaceAll("\\s", "");
		return textWithoutSpaces;
	}

	public static String removeRepeatedSymbols(String data) {
		StringBuffer textWithoutRepeatedSymbols = new StringBuffer();
		for (int i = 0; i < data.length(); i++) {
			String symbolToTest = Character.toString(data.charAt(i));
			boolean symbolNotFound = textWithoutRepeatedSymbols
					.indexOf(symbolToTest) == -1;
			if (symbolNotFound) {
				textWithoutRepeatedSymbols.append(symbolToTest);
			}
		}

		return textWithoutRepeatedSymbols.toString();

	}

	public static boolean containsUpperCaseSymbols(String data) {
		boolean upperCaseSymbolsFound = false;
		for (int i = 0; i < data.length() && !upperCaseSymbolsFound; i++) {
			char symbol = data.charAt(i);
			upperCaseSymbolsFound = Character.isUpperCase(symbol);
		}
		return upperCaseSymbolsFound;
	}

	public static String rotateString(String data, int offset) {
		char[] toRotate = data.toCharArray();
		char[] rotated = rotateArray(toRotate, offset);
		return new String(rotated);
	}

	private static char[] rotateArray(char[] toRotate, int offset) {
		int arrayLength = toRotate.length;
		char[] rotated = new char[arrayLength];
		for (int oldPosition = 0; oldPosition < arrayLength; oldPosition++) {
			int newPosition;
			if (offset >= 0) { // Rotating to the right
				newPosition = (oldPosition + offset) % arrayLength;
			} else { // Rotating to the left
				newPosition = (arrayLength + (oldPosition + offset))
						% arrayLength;
			}
			rotated[newPosition] = toRotate[oldPosition];
		}
		return rotated;
	}

}
